package com.course.assembler;

import com.course.entity.dao.Course;
import com.course.entity.dto.UserSearchBigCourseTypeDto;
import com.course.entity.dto.UserSearchDto;
import com.course.entity.dto.UserSearchSmallCourseTypeDto;
import com.course.filter.VisitDto;
import com.course.service.IBigCourseTypeService;
import com.course.service.ICourseService;
import com.course.service.ISmallCourseTypeService;
import com.course.service.ISubCampusCourseService;
import com.easyCode.feature.mybaits.CustomPage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 大忽悠
 * @create 2023/2/28 20:17
 */
@Component
@RequiredArgsConstructor
public class UserSearchCourseAssembler {
    private final ISubCampusCourseService iSubCampusCourseService;
    private final ICourseService iCourseService;
    private final IBigCourseTypeService iBigCourseTypeService;
    private final ISmallCourseTypeService iSmallCourseTypeService;

    public UserSearchDto userListCourse(CustomPage customPage) {
        //用户所属的分校区ID
        Integer cId = VisitDto.getCurrentVisit().getCId();
        //查询出当前分校区下拥有的所有课程
        List<Integer> courseIdList= iSubCampusCourseService.getCourseIdListOfUser(customPage, cId);
        if(ObjectUtils.isEmpty(courseIdList)){
            return null;
        }
        UserSearchDto userSearchDto = new UserSearchDto();
        //1.按照课程大类划分
        userSearchDto.setBigCourseTypeDtoList(doAssembleBigCourseTypeDto(courseIdList));
        return userSearchDto;
    }

    private List<UserSearchBigCourseTypeDto> doAssembleBigCourseTypeDto(List<Integer> courseIdList) {
        List<UserSearchBigCourseTypeDto> bigCourseTypeDtoList=new LinkedList<>();
        Map<Integer, List<Course>> courseGroupByBigCourseType = iCourseService.listByIds(courseIdList).stream().collect(Collectors.groupingBy(Course::getBigType));
        courseGroupByBigCourseType.forEach((bigCourseTypeId,courseList)->{
            UserSearchBigCourseTypeDto userSearchBigCourseTypeDto = new UserSearchBigCourseTypeDto(iBigCourseTypeService.getById(bigCourseTypeId));
            userSearchBigCourseTypeDto.setSmallCourseTypeDtos(doAssembleSmallCourseType(courseList));
            bigCourseTypeDtoList.add(userSearchBigCourseTypeDto);
        });
        return bigCourseTypeDtoList;
    }

    private List<UserSearchSmallCourseTypeDto> doAssembleSmallCourseType(List<Course> courseList) {
        //2.在当前课程大类基础上,按照课程小类继续划分
        List<UserSearchSmallCourseTypeDto> smallCourseTypeDtos=new LinkedList<>();
        Map<Integer, List<Course>> courseGroupBySmallCourseType = courseList.stream().collect(Collectors.groupingBy(Course::getSmallType));
        courseGroupBySmallCourseType.forEach((smallCourseTypeId,cList)->{
            UserSearchSmallCourseTypeDto userSearchSmallCourseTypeDto = new UserSearchSmallCourseTypeDto(iSmallCourseTypeService.getById(smallCourseTypeId));
            userSearchSmallCourseTypeDto.setCourseDtoList(courseList);
            smallCourseTypeDtos.add(userSearchSmallCourseTypeDto);
        });
        return smallCourseTypeDtos;
    }
}
